package com.cdut.service.serviceimp;

import com.cdut.dao.GoodDao;
import com.cdut.pojo.Good;
import com.cdut.service.GoodService;
import com.cdut.util.JsonResponce;
import com.cdut.util.PageSetting;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * author: xinmu
 * date: 2022/4/20
 * time: 21:08
 */

@Service
public class GoodServiceImpl implements GoodService {

    @Autowired
    GoodDao goodDao;


    @Override
    public JsonResponce addGood(Good good, MultipartFile multipartFile) throws IOException {
        ApplicationHome applicationHome = new ApplicationHome(this.getClass());
//        String path=applicationHome.getDir().getParentFile().getParentFile().getAbsolutePath() + "\\src\\main\\resources\\static\\updatafile\\"+good.getGname()+".jpg";
//        System.out.println(path);

        //String path="/home/lighthouse/myapp/updatafile/"+good.getGname()+".jpg";

        String path="/home/xinmu/code/mall/target/updatefile/"+good.getGname()+".jpg";

        System.out.println(path);
        File filepath=new File(path);
        if(!filepath.exists())
        {
            filepath.mkdirs();
            multipartFile.transferTo(filepath);
        }
        int code = goodDao.addGood(good);
        JsonResponce jsonResponce=new JsonResponce(code,"操作成功",null);
        if (code==0){
            jsonResponce.setMsg("操作失败");
        }
        return jsonResponce;
    }

    @Override
    public JsonResponce<Integer> deleteGood(int gid) {

        int code = goodDao.deleteGood(gid);
        JsonResponce<Integer> jsonResponce=new JsonResponce<>(code,"操作成功",null);
        if (code==0){
            jsonResponce.setMsg("操作失败");
        }

        return jsonResponce;
    }

    @Override
    public JsonResponce upDateGood(Good good) {

        int code = goodDao.upDateGood(good);
        JsonResponce jsonResponce = new JsonResponce<>(code, "操作成功", null);
        if (code==0){
            jsonResponce.setMsg("操作失败");
        }

        return jsonResponce;
    }

    @Override
    public JsonResponce<List<Good>> queryGoodByName(String name) {
        List<Good> goodList = null;
        goodList=goodDao.queryGoodByName(name);
        JsonResponce<List<Good>> jsonResponce = new JsonResponce<>(1, "操作成功", goodList);
        if (goodList.isEmpty()){
            jsonResponce.setCode(1);
            jsonResponce.setMsg("未查询到");
        }

        return jsonResponce;
    }

    @Override
    public JsonResponce<List<Good>> queryGoodList() {
        System.out.println("开始调用商品信息");
        List<Good> goodList =null;
        goodList=goodDao.queryGoodList();
        JsonResponce<List<Good>> jsonResponce = new JsonResponce<>(1, "操作成功", goodList);
        if (goodList.isEmpty()){
            jsonResponce.setMsg("无数据");
            jsonResponce.setCode(0);
        }

        return  jsonResponce;
    }

    @Override
    public JsonResponce<Good> queryGoodById(int gid) {
        Good good = goodDao.queryGoodById(gid);
        JsonResponce<Good> jsonResponce = new JsonResponce<>(1, "操作成功", good);
        if (good==null){
            jsonResponce.setMsg("无数据");
            jsonResponce.setCode(0);
        }

        return jsonResponce;
    }

    /**
     * 通过分类查询
     * @param tag 分类
     * @return 查询到的商品
     */
    @Override
    public JsonResponce<List<Good>> queryGoodByTag(String tag) {
        List<Good> goodList = goodDao.queryGoodByTag(tag);
        JsonResponce<List<Good>> jsonResponce=new JsonResponce<>(1,"操作成功",goodList);

        if (goodList==null){
            jsonResponce.setMsg("无数据");
            jsonResponce.setCode(0);
        }

        return jsonResponce;
    }

    /**
     * 通过标签和商品名查询
     * @param gname 商品名
     * @param tag 标签
     * @return good list
     */
    @Override
    public JsonResponce<List<Good>> queryGood(String gname,String tag){
        List<Good> goodList = goodDao.queryGood(gname, tag);
        JsonResponce<List<Good>> jsonResponce = new JsonResponce<>(1, "操作成功", goodList);

        if (goodList==null){
            jsonResponce.setMsg("无数据");
            jsonResponce.setCode(0);
        }
        return jsonResponce;
    }


    /**
     * 通过标签和商品名实现分页的查询
     * @param gname 商品名
     * @param tag 标签
     * @param pageNum 页码
     * @return list
     */
    @Override
    public List<Good> queryGoodPages(String gname, String tag,int pageNum,int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Good> goodList = goodDao.queryGood(gname, tag);
        return goodList;
    }

    /**
     * 分页查询全部的商品
     * @param pageNum 页码
     * @param pageSize 页面大小
     * @return
     */
    public List<Good> queryGoodListPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Good> goodList = goodDao.queryGoodList();
        return goodList;
    }

    /**
     * 标签分页查询
     * @param tag 标签
     * @param pageNum 页码
     * @param pageSize 页面大小
     * @return list
     */
    @Override
    public List<Good> queryGoodTagPage(String tag,int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Good> goodList = goodDao.queryGoodByTag(tag);
        return goodList;
    }


    /**
     * 查询用户的商品
     * @param uid 用户id
     * @return list
     */
    @Override
    public List<Good> queryGoodByUid(int uid) {

        List<Good> goodList = goodDao.queryGoodByUid(uid);

        return goodList;
    }
}
